﻿
if ((authority[0] | authority[1]) === 0) {
    alert("你没有权限查看此内容");
    window.location.href = "index.html";
}


var rawData = [//项目数据，第一项为项目编号
    [

    ],
    [

    ],
    [

    ]
];

if (authority[0] === 1) {
    taskMessage = {
        "requestType": "client",
        "pj_client": user.user_id
    };
} else if (authority[1] === 1) {
    taskMessage = {
        "requestType": "staff"
    };
}
var postData;
$.ajax({
    url: '/pj/project_list',
    type: 'post',
    data: taskMessage,
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            console.log(data)
            // window.location.href = '/projectManage.html';
        }

    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
//设置每一页的行数
var countPerPage=[];
//默认情况下
if(user.countPerPage== 0){
    countPerPage = [10, 10, 10];
    $('.rowNum').val(10)
}else {
    countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
    $('.rowNum').val(user.countPerPage)
}

//点击设置每一页行数的确定按钮
$('.rowsBut').click(function () {
    //获得文本框中输入的值
    var rowsNumbuer = $('.rowNum').val();
    //将该值传入到后台中
    $.ajax({
        url:'/pj/countPerPage',
        type:'post',
        data:{'rowsNumbuer': rowsNumbuer},
        async:false,
        success:function () {
            //console.log('点击确认以后的countPerPage'+user.userId)
            if(user.countPerPage== 0){
                countPerPage = [10, 10, 10];
                $('.rowNum').val(10)
            }else {
                countPerPage = [user.countPerPage,user.countPerPage,user.countPerPage];
                $('.rowNum').val(user.countPerPage)
            }
        },
        error:function () {
            if (status == 'error') {
                alert("你的网络有问题！");
            }
        }
    });
    //刷新页面
    location.reload()
})




// 用数据库查询结果替换原始的傻瓜数据
var dataAll = new Array();//“全部”
var dataUnfinished = new Array();//“进行中”
var dataFinished = new Array();//“已结束”
for (var i = 0; i < postData.length; i++) {
    var everyItem = new Array();//每一条数据
    var unfinishedFlag = 0;//默认已结束
    var stateIndex = 2;//“状态”的下标
    for (var d in postData[i]) {
        var everyValue = postData[i][d];
        //限制条件
        if (d === "pj_start" || d === "pj_fin") {
            everyValue = everyValue.substring(0, 10);
        }

        if ("pj_fin" === d) {//“结束时间”
            // 这里将来可能修改
            // 根据当前时间判断是否结束
            var t = new Date();
            var currentTime = t.getFullYear() + "-";
            var currentMonth = (t.getMonth() + 1 >= 10 ? t.getMonth() + 1 : "0" + (t.getMonth() + 1));
            currentTime = currentTime + currentMonth + "-";
            var currentDate = (t.getDate() < 10 ? "0" + t.getDate() : t.getDate());
            currentTime = currentTime + currentDate;
            // 日期格式：YYYY-MM-DD
            
            if (everyValue > currentTime) {//“进行中”
                unfinishedFlag = 1;
            }
            else {
                unfinishedFlag = 0;
            }
        }
        everyItem.push(everyValue);
    }
    dataAll.push(everyItem);
    if (unfinishedFlag === 1) { //“进行中”
        everyItem[stateIndex] = "进行中";
        dataUnfinished.push(everyItem);
    }
    else { //“已结束”
        everyItem[stateIndex] = "已结束"; 
        dataFinished.push(everyItem);
    }
}
//原始数据集合更新
rawData[2] = dataAll;
rawData[0] = dataUnfinished;
rawData[1] = dataFinished;
var dummyData = [].concat(rawData);//副本

var title;
var methodCount = new Array();
var neededOptionIndex;
//显示选项卡
var optionCard = document.getElementById("optionCard");
//初始化表格标题，根据权限类型动态展示新建按钮和选项卡
console.log(authority)
if (authority[0] === 1) {//用户权限
    optionCard.innerHTML = "<li class=\"active\"><a href=\"#lC\" data-toggle=\"tab\">全部</a></li>" +
        "<li><a href=\"#lA\" data-toggle=\"tab\">进行中</a></li>" +
        "<li><a href=\"#lB\" data-toggle=\"tab\">已结束</a></li>";
    title = [
        ["编号", "项目名称", "项目状态", "开始时间", "结束时间", "委托单列表", "详细信息"],
        ["编号", "项目名称", "项目状态", "开始时间", "结束时间", "委托单列表", "详细信息"],
        ["编号", "项目名称", "项目状态", "开始时间", "结束时间", "委托单列表", "详细信息"]];
    neededOptionIndex = [
        [1, 2],
        [1, 2],
        [1, 2]
    ];
    methodCount = [5, 5, 5];
}
else if (authority[1] === 1) {//业务权限
    optionCard.innerHTML = "<li class=\"active\"><a href=\"#lC\" data-toggle=\"tab\">全部</a></li>" +
        "<li><a href=\"#lA\" data-toggle=\"tab\">进行中</a></li>" +
        "<li><a href=\"#lB\" data-toggle=\"tab\">已结束</a></li>";
    title = [
        ["编号", "项目名称", "项目状态", "开始时间", "结束时间", "委托单位", "委托人", "委托单列表", "详细信息"],
        ["编号", "项目名称", "项目状态", "开始时间", "结束时间", "委托单位", "委托人", "委托单列表", "详细信息"],
        ["编号", "项目名称", "项目状态", "开始时间", "结束时间", "委托单位", "委托人", "委托单列表", "详细信息"]];
    //authority[12]==1表示审核权限，他不应该有新建项目的功能
    if (authority[12] == 0){
        document.getElementById("new").innerHTML = "<button onclick=\"window.location.href='newProject.html'\" class=\"btn btn-xs btn-success\"><i class=\"ace-icon fa fa-plus bigger-120\"> 新建项目</i></button>";
    }
    methodCount = [7, 7, 7];
    neededOptionIndex = [
        [1, 5, 6],
        [1, 5, 6],
        [1, 5, 6]
    ];
}

var pageID = [1, 1, 1];

var pageButtonCount = [8, 8, 8];
var pageCount = [parseInt((dummyData[0].length - 1) / countPerPage[0]) + 1,
parseInt((dummyData[1].length - 1) / countPerPage[1]) + 1,
parseInt((dummyData[2].length - 1) / countPerPage[2]) + 1];
var table = [document.getElementById("table0"), document.getElementById("table1"), document.getElementById("table2")];
var tbody = [table[0].children[1], table[1].children[1], table[2].children[1]];
var pages = [document.getElementById("pages0"), document.getElementById("pages1"), document.getElementById("pages2")];
console.log(tbody[0].innerHTML);

//初始化筛选栏可选项
for (let i = 0; i < neededOptionIndex.length; i++) {
    for (let j = 0; j < neededOptionIndex[i].length; j++) {
        let option = document.createElement("option");
        option.innerHTML = title[i][neededOptionIndex[i][j]];
        document.getElementById("searcher" + i).children[1].appendChild(option);
    }
}
//for (let i = 0; i < methodCount.length; i++) {
//    for (let j = 0; j < methodCount[i]; j++) {
//        let option = document.createElement("option");
//        option.innerHTML = title[i][j];
//        document.getElementById("searcher" + i).children[1].appendChild(option);
//    }
//}

    //初始化表格
    for (let i = 0; i < methodCount.length; i++) {
        let inner = "<td>&nbsp;</td>";
        for (let j = 0; j < title[i].length - 1; j++) {
            inner += "<td />";
        }
        for (let j = 0; j < countPerPage[i]; j++) {
            var tr = document.createElement("tr");
            tr.innerHTML = inner;
            tbody[i].appendChild(tr);
        }
    }
    for (let i = 0; i < methodCount.length; i++) {
        for (let j = 0; j < (pageCount[i] > pageButtonCount[i] ? pageButtonCount[i] : pageCount[i]); j++) {
            var li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + (j + 1) + ", " + i + ")");
            var a = document.createElement("a");
            a.innerHTML = j + 1;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[i].insertBefore(li, pages[i].lastElementChild);
        }
        for (let j = 0; j < title[i].length; j++) {
            let t = document.createElement("th");
            t.innerHTML = title[i][j];
            table[i].children[0].children[0].appendChild(t);
        }
        OnClick(1, i);
    }


// 生成翻页按钮
// console.log(pages[0].innerHTML);
function OnClick(index, cardIndex) {
    if (index === -1) {
        if (pageID[cardIndex] > 1) OnClick(pageID[cardIndex] - 1, cardIndex);
        return;
    }
    if (index === -2) {
        if (pageID[cardIndex] < pageCount[cardIndex]) OnClick(pageID[cardIndex] + 1, cardIndex);
        return;
    }
    if (index === 1 && dummyData[cardIndex].length < countPerPage[cardIndex]) {
        for (let i = 0; i < dummyData[cardIndex].length; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        for (let i = dummyData[cardIndex].length; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "display: none");
        }
        pages[cardIndex].setAttribute("style", "display: none");
    }
    else {
        for (let i = 0; i < countPerPage[cardIndex]; i++) {
            tbody[cardIndex].children[i].setAttribute("style", "");
        }
        pages[cardIndex].setAttribute("style", "");
    }
    pageID[cardIndex] = index;
    // console.log("c" + pageID);
    //显示数据
    for (let i = 0; i < countPerPage[cardIndex]; i++) {
        if (dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i] !== undefined) {
            for (let j = 0; j < methodCount[cardIndex]; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][j];
            }
            //显示操作按钮
            //用户查看自己的项目
            if (authority[0] === 1) {
                let button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                button.setAttribute("onclick", "window.location.href='/viewProject.html?id="
                    + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                button.innerHTML = "查看";
                tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;//倒数1

                button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                button.setAttribute("onclick", "window.location.href='/deputeManage.html?"
                    + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][1] + "'");//pao
                button.innerHTML = "查看";
                tbody[cardIndex].children[i].children[title[cardIndex].length - 2].innerHTML = button.outerHTML;//倒数2
            }//业务查看所有项目
            else if (authority[1] === 1) {
                let button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                button.setAttribute("onclick", "window.location.href='/viewProject.html?id="
                    + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][0] + "'");//pao
                button.innerHTML = "查看";
                tbody[cardIndex].children[i].children[title[cardIndex].length - 1].innerHTML = button.outerHTML;//倒数1

                button = document.createElement("a");
                button.setAttribute("href", "javascript:void(0)");
                button.setAttribute("onclick", "window.location.href='/deputeManage.html?id="
                    + dummyData[cardIndex][(index - 1) * countPerPage[cardIndex] + i][1] + "'");//pao
                button.innerHTML = "查看";
                tbody[cardIndex].children[i].children[title[cardIndex].length - 2].innerHTML = button.outerHTML;//倒数2
            }
        } else {
            tbody[cardIndex].children[i].children[0].innerHTML = "&nbsp;";
            for (let j = 1; j < title[cardIndex].length; j++) {
                tbody[cardIndex].children[i].children[j].innerHTML = "";
            }
        }
    }
    //刷新翻页按钮
    if (pageCount[cardIndex] > pageButtonCount[cardIndex]) {
        var first = parseInt(pageID[cardIndex] - pageButtonCount[cardIndex] / 2) + 1;
        if (first < 1) first = 1;
        if (first > pageCount[cardIndex] - pageButtonCount[cardIndex] + 1) first = pageCount[cardIndex] - pageButtonCount[cardIndex] + 1;
        for (let i = 0; i < pageButtonCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = first; i < first + pageButtonCount[cardIndex]; i++) {
            var li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + i + ", " + cardIndex + ")");
            if (i === pageID[cardIndex])
                li.setAttribute("class", "active");
            var a = document.createElement("a");
            a.innerHTML = i;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    } else {
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            pages[cardIndex].removeChild(pages[cardIndex].children[1]);
        }
        for (let i = 0; i < pageCount[cardIndex]; i++) {
            li = document.createElement("li");
            li.setAttribute("onclick", "OnClick(" + (i + 1) + ", " + cardIndex + ")");
            if (i + 1 === pageID[cardIndex])
                li.setAttribute("class", "active");
            a = document.createElement("a");
            a.innerHTML = i + 1;
            a.setAttribute("href", "#");
            li.appendChild(a);
            pages[cardIndex].insertBefore(li, pages[cardIndex].lastElementChild);
        }
    }
    // console.log("d" + pageID);
}

function OnSearch(cardIndex) {
    let searcher = document.getElementById("searcher" + cardIndex);//搜索栏对象
    let condition = searcher.children[1].selectedIndex;//下拉菜单中选择的那一项的下标
    let details = searcher.children[3].value;//检索的内容
    let timeRange = [searcher.children[5].value, searcher.children[7].value];//两个用来检索的时间，中间必定有一个逗号
    var handyData = rawData[cardIndex];//该页面上的信息
    var resultData = new Array();//存放检索结果
    for (let i = 0; i < handyData.length; i++) {
        //字符串匹配，长度为0则全匹配
        var regx = new RegExp(details);
        //if (regx.exec(handyData[i][condition])) {
        if (regx.exec(handyData[i][neededOptionIndex[cardIndex][condition]])) {
            if (searcher.children[5].value.length !== 0 && searcher.children[5].value > handyData[i][3])//“开始时间”
                continue;
            if (searcher.children[7].value.length !== 0 && searcher.children[7].value < handyData[i][3])//“结束时间”
                continue;
            resultData.push(handyData[i]);//加入符合的项            
        }
    }
    dummyData[cardIndex] = resultData;//替换，反正再返回当前页面会变回来

    OnClick(1, cardIndex);
}